<?php

namespace app\common\model\xiluhouse;

use app\common\model\xiluhouse\market\MarketDecoration;
use app\common\model\xiluhouse\market\MarketPurpose;
use think\Model;
use traits\model\SoftDelete;

class HouseIndexShow extends Base
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_house_index_show';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'image_text',
        'jump_type_text',
        'status_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getModeTypeList(){
        return ['1' => __('Mode_type 1'), '2' => __('Mode_type 2'), '3' => __('Mode_type 3')];
    }

    public function getJumpTypeList()
    {
        return ['1' => __('Jump_type 1'), '2' => __('Jump_type 2'), '3' => __('Jump_type 3'), '4' => __('Jump_type 4'), '5' => __('Jump_type 5'), '6' => __('Jump_type 6'), '7' => __('Jump_type 7'), '8' => __('Jump_type 8')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }

    public function getJumpTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['jump_type']) ? $data['jump_type'] : '');
        $list = $this->getJumpTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function housetype()
    {
        return $this->belongsTo('HouseType', 'house_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function housetag()
    {
        return $this->belongsTo('HouseTag', 'house_tag_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function rentMode()
    {
        return $this->belongsTo(RentalMode::class, 'rent_mode_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function rentTag()
    {
        return $this->belongsTo(HouseTag::class, 'rent_tag_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function marketPurpose()
    {
        return $this->belongsTo(MarketPurpose::class, 'market_purpose_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function marketDecoration()
    {
        return $this->belongsTo(MarketDecoration::class, 'market_decoration_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
